package com.meepo.basic.components

import android.content.Context
import android.graphics.Color
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target.SIZE_ORIGINAL
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.XPopupImageLoader
import com.meepo.basic.R
import java.io.File

// todo 迁移
object ImagePopup {

    fun popup(context: Context, imageView: ImageView, url: Any?) {
        val p = XPopup.Builder(context).isDestroyOnDismiss(true)
            .asImageViewer(imageView, url, object : XPopupImageLoader {
                override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                    //必须指定Target.SIZE_ORIGINAL，否则无法拿到原图，就无法享用天衣无缝的动画
                    Glide.with(imageView).load(uri).apply(RequestOptions().override(SIZE_ORIGINAL)).into(imageView)
                }

                override fun getImageFile(context: Context, uri: Any): File {
                    return Glide.with(context).downloadOnly().load(uri).submit().get()
                }
            })
        p.isInfinite(true)
        p.setPlaceholderColor(context.resources.getColor(R.color.default_background))
        p.setPlaceholderStrokeColor(context.resources.getColor(R.color.default_background))

        p.show()
    }
}